package com.example.auth.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.annotations.Autowired;
import com.example.annotations.Service;
import com.example.auth.model.DeptModel;
import com.example.auth.model.UserModel;
import com.example.auth.service.DeptModelService;
import com.example.auth.service.UserModelService;
import com.example.auth.uitls.UserModelMap;
import com.example.auth.uitls.UserModelUtils;
import com.example.db.SqlBuilder;
import com.example.db.TableService;
import com.example.db.model.IPage;
import com.example.utils.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @Classname UserModelServiceImpl
 * @Description TODO
 * @Date 2022/6/2 12:52
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@Service
public class UserModelServiceImpl implements UserModelService {

    @Autowired
    private TableService tableService;

    @Autowired
    private DeptModelService deptModelService;


    @Override
    public void deleteUserModelById(String id) {

    }

    @Override
    public UserModel saveUserModel(UserModel userModel) {
        tableService.save(userModel,UserModel.class);
        return userModel;
    }

    @Override
    public UserModel getUserModelById(String id) {
        UserModel userModel=tableService.getOneById(id,UserModel.class);
        if(userModel != null && userModel.getDeptId()!=null){
            DeptModel deptModel=deptModelService.getDeptModelById(userModel.getDeptId());
            userModel.setDeptName(deptModel.getDeptName());
        }
        return userModel;
    }


    @Override
    public UserModel updateUserModelById(UserModel userModel) {
        tableService.updateById(userModel,UserModel.class);
        return userModel;
    }

    @Override
    public UserModel getUserModel(String username) {
        SqlBuilder sqlBuilder=tableService.getSqlBuilder(UserModel.class);
        sqlBuilder.and("username").eq(username);
        return tableService.selectOne(sqlBuilder,UserModel.class);
    }

    @Override
    public IPage<UserModel> getUserModelPage(int pageNum, int pageSize, JSONObject object) {
      SqlBuilder sqlBuilder=tableService.getSqlBuilder(UserModel.class);
      if(object!=null){
          Object username=object.get("username");
          if(!StringUtils.isEmpty(username)){
              sqlBuilder.and("username").like(username);
          }
          Object idcard=object.get("idcard");
          if(!StringUtils.isEmpty(idcard)){
              sqlBuilder.and("idcard").like(idcard);
          }
          Object phone=object.get("phone");
          if(!StringUtils.isEmpty(phone)){
              sqlBuilder.and("phone").like(phone);
          }
          Object sex=object.get("sex");
          if(!StringUtils.isEmpty(sex)){
              sqlBuilder.and("sex").eq(sex);
          }
          Object realname=object.get("realname");
          if (!StringUtils.isEmpty(realname)) {
              sqlBuilder.and("realname").like(realname);
          }
          Object deptId=object.get("deptId");
          if (!StringUtils.isEmpty(deptId)) {
              List<DeptModel> deptModels=deptModelService.getDeptModelChildListById(deptId);
              List<String> deptIds=deptModels.stream().map(e->{
                  return String.valueOf(e.getId());
              }).collect(Collectors.toList());
              deptIds.add(String.valueOf(deptId));
              sqlBuilder.and("deptId").in(deptIds);
          }
      }
      sqlBuilder.page(pageNum,pageSize);
      return tableService.getPage(sqlBuilder,UserModel.class);
    }

    @Override
    public UserModel getUserModel() {
        return UserModelUtils.getCurrentUser();
    }

    @Override
    public UserModel initUserModel(UserModel userModel) {
        if(userModel==null){
            userModel = new UserModel();
        }
        Date date=new Date();
        userModel.setCreatedate(date);
        userModel.setIslocked(true);

        return userModel;
    }

    @Override
    public IPage<UserModel> getOnlineUserModel(Integer pageNum, Integer pageSize, JSONObject object) {
        List<UserModel> userModels= UserModelMap.getOnlineUserModels();
        if(object != null){
            Object idcard=object.get("idcard");
            if(!StringUtils.isEmpty(idcard)){
                userModels=userModels.stream().
                        filter(e->String.valueOf(e.getIdcard()).contains(String.valueOf(idcard))).collect(Collectors.toList());
            }
            Object phone=object.get("phone");
            if (!StringUtils.isEmpty(phone)) {
                userModels=userModels.stream()
                        .filter(e->String.valueOf(e.getPhone()).contains(String.valueOf(phone))).collect(Collectors.toList());
            }
            Object realname=object.get("realname");
            if (!StringUtils.isEmpty(realname)) {
                userModels=userModels.stream()
                        .filter(e->String.valueOf(e.getRealname()).contains(String.valueOf(realname))).collect(Collectors.toList());
            }
            Object username=object.get("username");
            if (!StringUtils.isEmpty(username)) {
                userModels=userModels.stream()
                        .filter(e->String.valueOf(e.getUsername()).contains(String.valueOf(username))).collect(Collectors.toList());
            }
            System.out.println(JSONObject.toJSONString(userModels));
        }
        int size=userModels.size();
        userModels=userModels.stream().skip((pageNum-1)*pageSize).limit(pageSize).
                collect(Collectors.toList());
        return new IPage(size,userModels,pageSize,pageNum);
    }

    @Override
    public void removeOnlineUserModelByUsername(String userName) {
        UserModelMap.removeTokenByUserName(userName);
    }

    @Override
    public List<UserModel> getUserModelList() {
        SqlBuilder sqlBuilder=tableService.getSqlBuilder(UserModel.class);
        return tableService.selectList(sqlBuilder,UserModel.class);
    }
}
